home *** CD-ROM | disk | FTP | other *** search
- // DrawDlgDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "DrawDlg.h"
- #include "DrawDlgDlg.h"
- #include "math.h"
- #include "pdrawconst.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDlgDlg dialog
-
- CDrawDlgDlg::CDrawDlgDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CDrawDlgDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDrawDlgDlg)
- m_uAngle = 0;
- m_fTicking = FALSE;
- m_fBorder = FALSE;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_lHHID = -1;
- m_lMMID = -1;
- m_lSSID = -1;
- m_fBorder = FALSE;
- m_nTimer = 0;
- }
-
- void CDrawDlgDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDrawDlgDlg)
- DDX_Control(pDX, IDC_POLARDRAWCTRL1, m_Canvas);
- DDX_Check(pDX, IDC_CHECK1, m_fTicking);
- DDX_Check(pDX, IDC_BORDER, m_fBorder);
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CDrawDlgDlg, CDialog)
- //{{AFX_MSG_MAP(CDrawDlgDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
- ON_BN_CLICKED(IDC_BORDER, OnBorder)
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDlgDlg message handlers
- CPoint ptCenter(0, 0);
-
-
- BOOL CDrawDlgDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- m_Canvas.SetEnableRendering(FALSE);
- m_Canvas.SetRecordUndo(FALSE);
-
- CRect rc;
- GetDlgItem(IDC_POLARDRAWCTRL1)->GetClientRect(&rc);
- m_Canvas.SetPageWidth(rc.Width()); //pixels
- m_Canvas.SetPageHeight(rc.Height()); //pixels
- m_Canvas.SetPageOrigin(0, 0); //pixels
- m_Canvas.SetViewportOrigin(0, 0); //pixels
-
- long l = 0;
- long lRad = 90;
- long lX, lY;
- long lID;
-
- while(l < 12)
- {
- float lAngle = (float)(l*30 / (360 / (2*3.1416)));
- lX = ptCenter.x - (long)(sin(lAngle) * lRad);
- lY = ptCenter.y - (long)(cos(lAngle) * lRad);
- if((l*30) % 90 == 0)
- lID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 10, lY -10, lX +10, lY+10);
- else
- lID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 8, lY -8, lX +8, lY+8);
-
- m_Canvas.SetShapeRotation(lID, l*30);
-
- m_Canvas.SetShapeFillColor(lID, RGB(0,0,255));
-
- l++;
- }
-
- lX = ptCenter.x;
- lY = ptCenter.y;
-
- // ID of hour handle
- m_lHHID = m_Canvas.AddShape(polLeftArrow, lX - 40, lY -30, lX +40, lY+30);
- m_Canvas.SetShapeRotation(m_lHHID, -90);
- m_Canvas.MoveShape(m_lHHID, 0, -30);
- // ID of minute handle
- m_lMMID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 10, lY -100, lX +10, lY+10);
- m_Canvas.SetShapeFillColor(m_lMMID, RGB(255,0,0));
- // ID of seconds handle
- m_lSSID = m_Canvas.AddShape(polRectangle, lX - 2, lY -95, lX +2, lY+10);
-
-
- m_Canvas.SetShapeFillColor(m_lSSID, RGB(0,0,255));
- m_Canvas.SelectAll();
- m_Canvas.FitToSelection();
- m_Canvas.ClearSelection();
-
-
- m_nTimer = SetTimer(1, 1000, NULL);
- // enable rendering from here
- m_Canvas.SetEnableRendering(TRUE);
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CDrawDlgDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CDrawDlgDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- void CDrawDlgDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent == m_nTimer)
- {
- CTime time = CTime::GetCurrentTime();
- long lHH = time.GetHour();
- long lMM = time.GetMinute();
- long lSS = time.GetSecond();
- m_Canvas.SetEnableRendering(FALSE);
-
- long lX = ptCenter.x;
- long lY = ptCenter.y;
-
- // set shapes to initial position
- m_Canvas.SetShapePos(m_lHHID, lX - 40, lY -30, lX +40, lY+30);
- m_Canvas.SetShapeRotation(m_lHHID, -90);
- m_Canvas.MoveShape(m_lHHID, 0, -30);
- m_Canvas.SetShapePos(m_lMMID, lX - 10, lY -100, lX +10, lY+10);
- m_Canvas.SetShapeRotation(m_lMMID, 0);
- m_Canvas.SetShapeFillColor(m_lMMID, RGB(255,0,0));
- m_Canvas.SetShapePos(m_lSSID, lX - 2, lY -95, lX +2, lY+10);
- m_Canvas.SetShapeRotation(m_lSSID, 0);
-
- // rotate them around center according to the current time
- m_Canvas.RotateShapeAroundPoint(m_lHHID, ptCenter.x, ptCenter.y, -(long)((float(lHH + lMM/60.0)) * 30));
- m_Canvas.RotateShapeAroundPoint(m_lMMID, ptCenter.x, ptCenter.y, -lMM * 6 );
- m_Canvas.RotateShapeAroundPoint(m_lSSID, ptCenter.x, ptCenter.y, -lSS * 6 );
-
- long crVal = (long)(255 * ((lSS % 60) / 60.0));
- m_Canvas.SetEnableRendering(TRUE);
- m_Canvas.SetShapeFillColor(m_lHHID, RGB(crVal, 255, 0));
- if(m_fTicking)
- MessageBeep(1);
- }
- CDialog::OnTimer(nIDEvent);
- }
-
- void CDrawDlgDlg::OnOK()
- {
- CDialog::OnOK();
- }
-
- void CDrawDlgDlg::OnCheck1()
- {
- UpdateData(TRUE);
- }
-
- void CDrawDlgDlg::OnBorder()
- {
- UpdateData(TRUE);
-
- // change the style of border:
- if(m_fBorder)
- {
- m_Canvas.SetAppearance(1);
- m_Canvas.SetBorderStyle(1);
- }
- else
- {
- m_Canvas.SetAppearance(0);
- m_Canvas.SetBorderStyle(0);
- }
-
- // stretch the canvas to fit the window
- m_Canvas.SetEnableRendering(FALSE);
- m_Canvas.SelectAll();
- m_Canvas.FitToSelection();
- m_Canvas.SetEnableRendering(TRUE);
- // this call will re-render canvas
- m_Canvas.ClearSelection();
-
-
- }
-
- void CDrawDlgDlg::OnDestroy()
- {
- CDialog::OnDestroy();
-
- // Now, kill the timer that we created
- if(m_nTimer != 0)
- {
- KillTimer(m_nTimer);
- }
- }
-